package com.fj.netty.rpc.server;


import com.fj.netty.rpc.server.handler.RpcRequestMessageHandler;
import com.fj.netty.rpc.protocol.RpcMessageCodec;
import com.fj.netty.rpc.protocol.RpcMessageLengthFieldDecoder;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * @author fj
 * @date 2024/1/17 17:10
 */
public class RpcServer {
    public static void main(String[] args) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(2);
        try {
            Channel channel = new ServerBootstrap()
                    .group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new RpcMessageLengthFieldDecoder());
                            ch.pipeline().addLast(new RpcMessageCodec());
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                            ch.pipeline().addLast(new RpcRequestMessageHandler());

                        }
                    })
                    .bind(6789).sync().channel();
            ChannelFuture sync = channel.closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

    }
}
